
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		idsObj: [], //组织架构人员 被选中 的集合
		treeChooseStatus: [], //组织架构部门（选中+未选中）集合
		tipsOptions: "", //组织架构人员选择框熟悉 
		pageTempParams: '',
		formClassName:'' //表单的样式
	},
	getters: {
		getIds: state => {
			return state.idsObj
		},
		className:state=>{
			return state.formClassName
		}
	},
	mutations: {
		idsObjIncrease(state, newObj) {
			let obj = {
				id: newObj.id,
				name: newObj.teacherName || newObj.name,
				departmentId: newObj.departmentId,
				jobNumber: newObj.jobNumber
			}
			state.idsObj.push(obj)
		},
		idsObjDecrease(state, newObj) {
			let index = ''
			state.idsObj.forEach((item, i) => {
				if (item.id == newObj.id) {
					index = i
				}
			})
			state.idsObj.splice(index, 1)
			state.treeChooseStatus.forEach((item, i) => {
				if (item.id == newObj.departmentId) {
					state.treeChooseStatus.splice(i, 1)
				}
			})
			debugger
		},
		changeTree(state, newArr) {
			state.treeChooseStatus = newArr
		},
		changeTreeChooseStatus(state, obj) {
			let index;
			state.treeChooseStatus.forEach((item, i) => {
				if (item.id == obj.id) {
					index = i
				}
			})
			if (obj.chooseKey) {
				state.treeChooseStatus.push(obj)
			} else {
			   state.treeChooseStatus.splice(index, 1)
			}
			// if (obj) {
			// 	state.treeChooseStatus.forEach((item, i) => {
			// 		if (item.id == obj.id) {
			// 			state.treeChooseStatus[i].chooseKey = obj.chooseKey
			// 		}
			// 	})
			// }
		},
		updateTipsOptions(state, options) {
			state.tipsOptions = options
		},
		//清空选中人员，应用与于组织架构单选时期
		clearIdsObj(state) {
			state.idsObj = []
		},
		/**
		 * 保存页面临时变量
		 * @param {Object} params 临时变量
		 * params.name 页面名
		 * */
		savePageTempParams(state, params) {
			state.pageTempParams = params;
		},
		claearPageTempParams(state) {
			state.pageTempParams = ''
		},
		setClassName(state,className){
			state.formClassName = className;
		}

	},
	actions: {
		idsObjIncrease(context, newObj) {
			context.commit('idsObjIncrease', newObj)
		},
		idsObjDecrease(context, newObj) {
			context.commit('idsObjDecrease', newObj)
		},
		changeTree(context, newArr) {
			context.commit('changeTree', newArr)
		},
		changeTreeChooseStatus(context, obj) {
			context.commit('changeTreeChooseStatus', obj)
		},
		updateTipsOptions(context, obj) {
			context.commit('updateTipsOptions', obj)
		},
		setClassName(context, obj) {
			context.commit('setClassName', obj)
		},
		clearIdsObj(context) {
			context.commit('clearIdsObj')
		}

	}
})
export default store
